package com.anysoft.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anysoft.MentorConst;
import com.anysoft.R;
import com.anysoft.base.BaseActivity;
import com.anysoft.bean.UserBean;
import com.anysoft.event.GestureEvent;
import com.anysoft.view.ViewLoginNinePointPwd;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.anysoft.R.id.ll_login_pwd_bottom;
import static com.anysoft.R.id.ll_login_pwd_view;
import static com.anysoft.R.id.tv_pass;

/**
 * Created by wwq on 2017/1/18.
 */

public class SetGestureStep1Activity extends BaseActivity {


    public static int PWD_WIDTH = 0;
    public static int PWD_HEIGHT = 0;
    protected ViewLoginNinePointPwd nv;// 九宫格View
    @BindView(tv_pass)
    TextView tvPass;
    @BindView(ll_login_pwd_bottom)
    LinearLayout llLoginPwdBottom;
    @BindView(R.id.tv_set_gesture)
    TextView tvSetGesture;
    @BindView(R.id.ll_login_pwd_top)
    LinearLayout llLoginPwdTop;
    @BindView(ll_login_pwd_view)
    LinearLayout llLoginPwdView;
    @BindView(R.id.ll_pass)
    LinearLayout llPass;

    private String pwd = "";
    private UserBean userBean;

    private boolean setSuccess = false;
    private boolean isChangeGesturePwd = false;

    private boolean isShowSkip;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_set_gesture_step);
        ButterKnife.bind(this);

        tvSetGesture.setText("为了您的账户安全，请设置手势密码");

        pwd = getIntent().getStringExtra(MentorConst.GET_PASSWORD);
        isShowSkip = getIntent().getBooleanExtra(MentorConst.IS_SHOW_SKIP, true);
        userBean = (UserBean) getIntent().getSerializableExtra(MentorConst.GET_USERBEAN);

        if (isShowSkip) {
            tvPass.setVisibility(View.VISIBLE);
        } else {
            llPass.setVisibility(View.VISIBLE);
        }

        llPass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        isChangeGesturePwd = getIntent().getBooleanExtra(MentorConst.IS_CHANGE_GESTURE_PWD, false);

        measure();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    public void measure() {
        WindowManager wm = (WindowManager) getBaseContext().getSystemService(
                Context.WINDOW_SERVICE);

        int height = wm.getDefaultDisplay().getHeight();
        PWD_WIDTH = wm.getDefaultDisplay().getWidth();
        PWD_HEIGHT = height;

        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);

        llLoginPwdTop.measure(w, h);
        PWD_HEIGHT = PWD_HEIGHT - llLoginPwdTop.getMeasuredHeight();

        llLoginPwdBottom.measure(w, h);
        PWD_HEIGHT = PWD_HEIGHT - llLoginPwdBottom.getMeasuredHeight();

        if (PWD_WIDTH != 0 && PWD_HEIGHT != 0) {
            init();
        }
    }

    protected void init() {
        nv = new ViewLoginNinePointPwd(this, PWD_WIDTH, PWD_HEIGHT);
        llLoginPwdView.addView(nv);

        nv.SetOnPwdSetSucessListener(new ViewLoginNinePointPwd.OnPwdSetSucessListener() {
            @Override
            public void OnPwdSetSucessListener(String password) {
//                Logger.t(TAG).d("password---" + password);
                Intent intent = new Intent().setClass(SetGestureStep1Activity.this, SetGestureStep2Activity.class);
                intent.putExtra(MentorConst.GET_PASSWORD, pwd);
                intent.putExtra(MentorConst.SET_GESTURE_PASSWORD, password);
                intent.putExtra(MentorConst.IS_SHOW_SKIP, isShowSkip);
                intent.putExtra(MentorConst.GET_USERBEAN, userBean);
                intent.putExtra(MentorConst.IS_CHANGE_GESTURE_PWD, isChangeGesturePwd);
                startActivity(intent);

                setSuccess = true;

                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (!isChangeGesturePwd) {
            if (!setSuccess) {
                EventBus.getDefault().post(new GestureEvent(false));
            }
        }
    }

    @OnClick(tv_pass)
    public void onClick() {
        finish();
    }
}
